<?php

if(!defined("Yourphp")) exit("Access Denied");
class TeacherAction extends BaseAction
{
    function _initialize()
    {	
		parent::_initialize();
		if(!$this->_userid){
			$this->assign('jumpUrl',U('User/Login/index'));
			$this->error(L('nologin'));
			exit;
        }
    }
    public function subject()
    {
        $arr['danxuan'] = M('question_bank')->where(array('userid'=>$this->_userid,'type'=>1))->count();
        $arr['duoxuan'] = M('question_bank')->where(array('userid'=>$this->_userid,'type'=>2))->count();
        $arr['panduan'] = M('question_bank')->where(array('userid'=>$this->_userid,'type'=>3))->count();
        $arr['tiankong'] = M('question_bank')->where(array('userid'=>$this->_userid,'type'=>4))->count();
        $arr['zhuguan'] = M('question_bank')->where(array('userid'=>$this->_userid,'type'=>5))->count();
        $this->assign('arr',$arr) ;
        
         $count = M('question_bank')->where(array('userid'=>$this->_userid))->count();
        if ($count > 0) {
            import("@.ORG.Page");
            //创建分页对象
            $listRows = 10;
            $page = new Page($count, $listRows);
            $voList = M('question_bank')->order('id desc')->where(array('userid'=>$this->_userid))->limit($page->firstRow . ',' . $page->listRows)->select();
            //分页跳转的时候保证查询条件
            foreach($voList as $key=>$value){
                $voList[$key]['answeroptions'] = explode('|', $value['answeroptions']);
            }

            $page = $page->show();

            $this->assign('list', $voList);
            $this->assign('page', $page);
        }

        $this->assign('total',$count) ;
        $this->display();
    }
    
    //生成试卷页面
        public function savequestion() {
        $data['type'] = $_POST['type'];
        $data['questiontype'] = 1;
        $data['content'] = $_POST['content'];
        if (!empty($_POST['answeroption'])) {
            foreach ($_POST['answeroption'] as $key => $value) {
                $data['answeroptions'] .= $key . '.' . $value . '|';
            }
        }
        if($_POST['type'] == 2){
            $data['answer'] = implode(',',json_decode($_POST['answer'],true)) ;
        }else{
            $data['answer'] = $_POST['answer'] ;
        }
        $data['status'] = 1;
        $data['lang'] = 1;
        $data['userid'] = $this->_userid;
        $data['username'] = $this->_username;
        $data['createtime'] = time();
        $info = M('question_bank')->add($data);
        if ($info) {
            echo '<script type="application/javascript">alert("添加成功");window.location.href="index.php?g=User&m=Teacher&a=subject"</script>';
            exit;
        } else {
            echo '<script type="application/javascript">alert("添加失败");window.location.href="index.php?g=User&m=Teacher&a=subject"</script>';
            exit;
        }
    }
    
    //删除question
    public function deletequestion() {
        $aa = $_POST['aa'];
        $ac = M('question_bank')->where(array('id' => $aa))->delete();
        if ($ac) {
            echo json_encode(array('code' => '0000', 'msg' => '添加成功'));
            exit;
        } else {
            echo json_encode(array('code' => '0001', 'msg' => '添加失败'));
            exit;
        }
    }

    public function make()
    {
        //$a=array('id'=>'1','name'=>'专业1','gwlist'=>array('id'=>'2','name'=>'岗位1','xmlist'=>array('id'=>'3','name'=>'项目1')));
        //echo json_encode($a);
        //exit;
        $ku=array();
        $qx=M('access_zhuanye')->where(array('uid'=>$this->_userid))->select();
        foreach ($qx as $key => $value) {
            $zy=M('zhuanye')->field('id,title')->where(array('id'=>$value['zyid']))->find();
            foreach ($zy as $key2 => $value2) {
                $gw=M('gangwei')->field('id,title')->where(array('zyid'=>$value2['id']))->select();
                foreach ($gw as $key3 => $value3) {
                    $xm=M('f_item')->field('id,title')->where(array('gwid'=>$value3['id']))->select();
                    
                        $gw[$key3]['xmlist']=$xm;
                    
                    $z=array('id'=>$zy['id'],'title'=>$zy['title'],'gwlist'=>$gw);
                     
                }
                 
            }
            array_push($ku,$z);
        }
        $this->assign('zyjson',json_encode($ku));
 
    //    $a=array('que'=>array('1','2'),'sco'=>array('dxz'=>'1','dx'=>'2','pd'=>'2','tk'=>'2','zg'=>'2'));
    //    echo json_encode($a);exit;
        $this->display();
    }
    //题目录post
    public function questionlist()
    {
        $xmid=0;
        $questiontype=$_GET['questiontype'];
        $content=$_GET['content'];
        if($questiontype==0)
        {
            $xmid=$_GET['xmid'];

        }
        else
        {
            $where['userid']=$this->_userid;
        }

        $where['questiontype']=$questiontype;
        $where['fitem']=$xmid;
         
        $where['content']=array('like','%'.$content.'%');
         
        $list=M('question_bank')->field('id,content,type,answeroptions,answer')->where($where)->select();
        foreach ($list as $key => $value) {
            if($value['type']==3)
            {
                $list[$key]['answeroptions']='A.正确|B.错误|';
            }
        }
        $this->ajaxReturn($list);
    }
    public function buildexam()
    {
        $title=$_POST['title'];
        $dx=$_POST['dx'];
        $dxz=$_POST['dxz'];
        $pd=$_POST['pd'];
        $tk=$_POST['tk'];
        $zg=$_POST['zg'];
        $type=$_POST['type'];
        $pid=$_POST['pid'];
        $total=$_POST['total'];
        $totaltime=0;
        $tasktime=0;
        if($type==0)
        {
            $totaltime=$_POST['totaltime'];
        }
        else
        {
            $tasktime=strtotime($_POST['tasktime']);
        }
        $question=array('que'=>json_decode($pid,true),'sco'=>array('dx'=>$dx,'dxz'=>$dxz,'pd'=>$pd,'tk'=>$tk,'zg'=>$zg,'total'=>$total));
        $data['userid']=$this->_userid;
        $data['username']=$this->_username;
        $data['createtime']=time();
        $data['updatetime']=time();
        $data['question']=json_encode($question);
        $data['title']=$title;
        $data['type']=$type;
        $data['totaltime']=$totaltime;
        $data['tasktime']=$tasktime;
        $add=M('exam')->add($data);
         
        // 
        //print_r(json_encode($question));json_encode($question)
        // $radd=M('exam')->add(array('userid'=>$this->_userid,'username'=>$this->$_username,'createtime'=>time(),'updatetime'=>time(),'question'=>'','title'=>$title,'type'=>$type,'totaltime'=>$totaltime,'tasktime'=>$tasktime));
        // echo $radd;exit;
        if($add)
        {
            $this->ajaxReturn('','成功',1);
        }
        else
        {
            $this->ajaxReturn('','失败',0);
        }
         
    }
    public function ExamCollect()
    {
        $exam=M('exam')->where(array('userid'=>$this->_userid,'type'=>0))->select();
        $list=array();
        foreach ($exam as $key => $value) {
            $total=json_decode($value['question'],true)['sco']['total'];
            //总人数
            $pcount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>0))->count();
            //完成 //包括批改的
            $wccount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>0,'status'=>array('GT',0)))->count();
            //批改
            $pgcount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>0,'status'=>2))->count();
            $fbcount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>0,'status'=>3))->count();
            $a=array('id'=>$value['id'],'title'=>$value['title'],'totaltime'=>$value['totaltime'],'total'=>$total,'pcount'=>$pcount,'wccount'=>$wccount,'pgcount'=>$pgcount,'fbcount'=>$fbcount);
            array_push($list,$a);
        }
        $this->assign('list',$list);
        $this->display();
    }
    public function TaskCollect()
    {
        $exam=M('exam')->where(array('userid'=>$this->_userid,'type'=>1))->select();
        $list=array();
        foreach ($exam as $key => $value) {
            $total=json_decode($value['question'],true)['sco']['total'];
            //总人数
            $pcount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>1))->count();
            //完成 //包括批改的
            $wccount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>1,'status'=>array('GT',0)))->count();
            //批改
            $pgcount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>1,'status'=>2))->count();
            $fbcount=M('exam_record')->where(array('eid'=>$value['id'],'type'=>1,'status'=>3))->count();
            $a=array('id'=>$value['id'],'title'=>$value['title'],'tasktime'=> date('Y-m-d H:i:s',$value['tasktime']),'total'=>$total,'pcount'=>$pcount,'wccount'=>$wccount,'pgcount'=>$pgcount,'fbcount'=>$fbcount);
            array_push($list,$a);
        }
        $this->assign('list',$list);
        $this->display();

    }
    public function ExamMark()
    {
        $eid=$_GET['eid'];//试卷id
        $exid=$_GET['exid'];//答卷id
        $userid = $_GET['uid'];
        if($exid)
        {
            if($userid){
                $examr=M('exam_record')->where(array('id'=>$exid,'eid'=>$eid,'user_id'=>$userid))->find();
            }else{
                $examr=M('exam_record')->where(array('id'=>$exid,'eid'=>$eid))->find();
            }
        }
        else
        {
            $nexid=$_GET['nexid'];
            if($nexid)
            {
                $examr=M('exam_record')->where(array('eid'=>$eid,'id'=>array('GT',$nexid)))->find();
            }
            else
            {
                $examr=M('exam_record')->where(array('eid'=>$eid))->find();
            }
            // $ccc=M('exam_record')->where(array('eid'=>$eid,'status'=>1))->count();
            // if($ccc>0)
            // {
                 
                //$examr=M('exam_record')->where(array('eid'=>$eid))->find();
        }
       

        $exam=M('exam')->where(array('id'=>$eid))->find();
        $question=json_decode($exam['question'],true);
        $eanswers=json_decode($examr['answers'],true);
        $examnote=json_decode($examr['exam_note'],true);
        if($userid){
            $uid = M('user')->where(array('id'=>$userid))->find();
        }else{
            $uid=M('user')->where(array('id'=>$examr['user_id']))->find();
        }
        $zg=array();
        $tm=$question['que'];
        $kgtotal=0;
        foreach ($tm as $key => $value) {
            $que=M('question_bank')->where(array('id'=>$value))->find();
            $tans='';
            $pfs='';
            foreach ($eanswers as $key2 => $value2) {
                   if($key2==$que['id'])
                   {
                        if(is_array($value2))
                        {
                            $tans=$value2;
                        }
                        else
                        {
                            $tans= array($value2);
                        }
                        break;
                   }
            }
            foreach ($examnote as $key3 => $value3) {
                if($key3==$que['id'])
                {
                    $pfs=$value3;
                    break;
                }
            }
            if($que['type']!='5')
            {
                if($examr['kg_score']==''||$examr['kg_score']==0)
                {
                    if(!$tans)
                    {
                        $tans=array($tans);
                    }
                    //print_r(explode(',',trim($que['answer'],',')));
                      //print_r($tans);
                    $r=$this->duicuo(explode(',',trim($que['answer'],',')),$tans);
                    //echo $r;
                    if($r)
                    {
                        switch ($que['type']) {
                            case '1':
                                $kgtotal+=$question['sco']['dx'];
                                break;
                            case '2':
                                $kgtotal+=$question['sco']['dxz'];
                                break;
                            case '3':
                                $kgtotal+=$question['sco']['pd'];
                                break;
                            case '4':
                                $kgtotal+=$question['sco']['tk'];
                                break;
                        }
                    }
                }
                else
                {
                    $kgtotal=$examr['kg_score'];
                }
            }
            else
            {
                array_push($zg,array('id'=>$que['id'],'content'=>$que['content'],'tans'=>$tans[0],'pfs'=>$pfs));
            }
        }
       
        if($examr['zg_score']==''||$examr['zg_score']==0)
        {
            $zgscore=0;
        }
        else
        {
            $zgscore=$examr['zg_score'];
        }
        $laud = M('laud')->where(array('user_id'=>$this->_userid,'exid'=>$examr['id']))->find();
        $c_comment = M('comment_company')->where(array('examrecordid'=>$examr['id'],'studentid'=>$examr['user_id']))->select();
        foreach($c_comment as $k=>$v){
            $name = M('user')->where(array('id'=>$v['companyid']))->getField('realname');
            $c[$k]['name'] = $name;
            $c[$k]['comment'] = $v['comment'];
        }
        foreach($c as $k=>$v){
             if(!isset($co[$v['name']])){
                $co[$v['name']][]=$v;
            }else{
                $co[$v['name']][]=$v;
            }
        }
        $this->assign('userid',$this->_userid);
        $this->assign('zan_num',$examr['laud']);
        $this->assign('laud',$laud);
        $this->assign('exam',$exam);
        $this->assign('co',$co);
        $this->assign('list',array('eid'=>$eid,'exid'=>$examr['id'],'userid'=>$examr['user_id'],'zg'=>$zg,'kgtotal'=>$kgtotal,'zgscore'=>$zgscore,'username'=>$uid['username']));
        $this->display();
    }
    public function AjaxExam()
    {
        $eid=$_GET['eid'];//试卷id
        $exid=$_GET['exid'];//答卷id
        $exam=M('exam')->where(array('id'=>$eid))->find();
        $examr=M('exam_record')->where(array('id'=>$exid))->find();
        $question=json_decode($exam['question'],true);
        $eanswers=json_decode($examr['answers'],true);
        $dx=array();
        $dxz=array();
        $pd=array();
        $tk=array();
        $tm=$question['que'];
        foreach ($tm as $key => $value) {
            $que=M('question_bank')->where(array('id'=>$value))->find();
            $tans='';
             
            foreach ($eanswers as $key2 => $value2) {
                   if($key2==$que['id'])
                   {
                       if(is_array($value2))
                       {
                            $tans=implode($value2,',');
                       }
                       else
                       {
                             $tans= $value2;
                        }
                       break;
                   }
            }
             
            $b=explode('|',trim($que['answeroptions'],'|'));
            
            switch ($que['type']) {
                
                case '1':
                    array_push($dx,array('content'=>$que['content'],'opt'=>$b,'answer'=>$que['answer'],'tans'=>$tans));
                    break;
                case '2':
                    array_push($dxz,array('content'=>$que['content'],'opt'=>$b,'answer'=>$que['answer'],'tans'=>$tans));
                    break;
                case '3':
                    array_push($pd,array('content'=>$que['content'],'opt'=>$b,'answer'=>$que['answer'],'tans'=>$tans));
                    break;
                case '4':
                    array_push($tk,array('content'=>$que['content'],'opt'=>$b,'answer'=>$que['answer'],'tans'=>$tans));
                    break;
            }
        }
        // dump(array('dx'=>$dx,'dxz'=>$dxz,'pd'=>$pd,'tk'=>$tk));
        // exit;
        $this->assign('list',array('dx'=>$dx,'dxz'=>$dxz,'pd'=>$pd,'tk'=>$tk));
        $this->display();
    }
    public function AjaxList()
    {
        //
        $eid=$_GET['eid'];//试卷id
        $usid=$_GET['userid'];//当前学生id
        $exr=M('exam_record')->where(array('eid'=>$eid,'user_id'=>array('NEQ',$usid)))->select();
        $list=array();
        foreach ($exr as $key => $value) {
            $uid=M('user')->where(array('id'=>$value['user_id']))->find();
            array_push($list,array('username'=>$uid['username'],'eid'=>$value['eid'],'id'=>$value['id'],'total_score'=>$value['total_score']));
        }
       
        $this->assign('list',$list);
        $this->display();
    }
    public function pingjuan()
    {
        $eid=$_GET['eid'];//试卷id
        $exid=$_GET['exid'];//答卷id
        $examnote=$_POST['examnote'];
        $kgsco=$_POST['kgsco'];
        $zgscore=0;
        $exnj=json_decode($examnote,true);
        foreach ($exnj as $key => $value) {
           $zgscore+=$value['sco'];
        }
        $total_score=$kgsco+$zgscore;
        $updata=M('exam_record')->where(array('id'=>$exid,'eid'=>$eid))->save(array('up_time'=>time(),'status'=>2,'exam_note'=>$examnote,'kg_score'=>$kgsco,'zg_score'=>$zgscore,'total_score'=>$total_score));
        if($updata)
        {
            $ccc=M('exam_record')->where(array('eid'=>$eid,'status'=>array('ELT',1)))->count();
            if($ccc>0)
            {
                //$cc=M('exam_record')->where(array('eid'=>$eid,'status'=>array('LT',3)))->find;
                $this->ajaxReturn(array('eid'=>$eid,'nexid'=>$exid),'成功,下一个',2);
            }
            else
            {
                $this->ajaxReturn('','成功,全部批改完',1);
            }
             
        }
        else
        {
            $this->ajaxReturn('','失败',0);
        }

    }
    public function relres()
    {
        $eid=$_POST['eid'];
        $fb=M('exam_record')->where(array('eid'=>$eid))->save(array('status'=>3));
        if($fb)
        {
            $this->ajaxReturn('','成功',1);
        }
        else
        {
            $this->ajaxReturn('','失败',0);
        }
    }
    public function AddExam()
    {
        $eid=$_GET['eid'];//试卷id
        $exam=M('exam')->where(array('id'=>$eid))->find();
        $user=M('user')->where(array('id'=>$this->_userid))->find();
        
         
       
        if($user['groupid']==15)
        {
            $myexamr=M('exam_record')->where(array('eid'=>$eid,'user_id'=>$this->_userid))->count();
            if($myexamr==0)
            {
                if($exam['userid']==$user['operator'])
                {
                    $examr=M('exam_record')->where(array('eid'=>$eid,'status'=>3))->count();
                    if($examr==0)
                    {
    
                        $add=M('exam_record')->add(array('eid'=>$eid,'user_id'=>$this->_userid,'username'=>$this->_username,'status'=>0,'type'=>$exam['type']));
                        if($add)
                        {
                            if($exam['type']=='1')
                            {
                                echo '<script type="application/javascript">alert("添加成功");window.location.href="index.php?g=User&m=Student&a=homework_list"</script>';
                            }
                            else
                            {
                                echo '<script type="application/javascript">alert("添加成功");window.location.href="index.php?g=User&m=Student&a=index"</script>';
                            }
                        
                            
                            exit;
                        }
                    }
                    else
                    {
                        if($exam['type']=='1')
                            {
                                echo '<script type="application/javascript">alert("该试卷已经不能添加");window.location.href="index.php?g=User&m=Student&a=homework_list"</script>';
                            }
                            else
                            {
                                echo '<script type="application/javascript">alert("该试卷已经不能添加");window.location.href="index.php?g=User&m=Student&a=index"</script>';
                            }
                    }
                }
                else
                {
                    if($exam['type']=='1')
                    {
                        echo '<script type="application/javascript">alert("不是该老师的学生");window.location.href="index.php?g=User&m=Student&a=homework_list"</script>';
                    }
                    else{
                        echo '<script type="application/javascript">alert("不是该老师的学生");window.location.href="index.php?g=User&m=Student&a=index"</script>';
                    }
                    exit;
                }
            }
            else
            {
                if($exam['type']=='1')
                {
                    echo '<script type="application/javascript">alert("领取过");window.location.href="index.php?g=User&m=Student&a=homework_list"</script>';
                }
                else{
                    echo '<script type="application/javascript">alert("领取过");window.location.href="index.php?g=User&m=Student&a=index"</script>';
                }
                exit;
            }
        }
        else
        {
            if($exam['type']=='1')
            {
                echo '<script type="application/javascript">alert("不是学生");window.location.href="index.php?g=User&m=Student&a=homework_list"</script>';
            }
            else
            {
                echo '<script type="application/javascript">alert("不是学生");window.location.href="index.php?g=User&m=Student&a=index"</script>';
            }
            exit;
        }
        //$add=M('exam_record')->add();
    }
    public function dowmfilezip()
    {
        $eid=$_POST['eid'];
        $ecount=M('exam_record')->where(array('eid'=>$eid))->count();
        $path=array();
        if($ecount>0)
        {
            $elist=M('exam_record')->where(array('eid'=>$eid))->select();
            foreach ($elist as $key => $value) {
                if($value['zg_uploads'])
                    {
                        $uli=M('user')->where(array('id'=>$value['user_id']))->find();
                        $file=".".$value['zg_uploads'];
                        preg_match('/\/([^\/]+\.[a-z]+)[^\/]*$/',$file,$match); 
                        $filename = explode(".", trim($match[0],'/'));
                        array_push($path,array('file'=>$file,'oldname'=>$filename[0].'.'.$filename[1],'name'=>$uli['realname'].'.'.$filename[1]));
                    }
            }
            if(count($path)>0)
            {
                $enam=M('exam')->where(array('id'=>$eid))->find();
                if($enam['type']==0)
                {
                    $newname=$eid.'exam'.count($path);
                }
                else
                {
                    $newname=$eid.'homework'.count($path);
                }
               
                $zipName = "./tzip/".$newname.".zip";//压缩包
                $arr=$this->zipdown($path,$zipName);
                if($arr['result'])
                {
                    //$this->ajaxReturn(array('file'=>$zipName,'name'=>$newname.".zip",'info'=>$arr['msg']));
                    $this->ajaxReturn(array('file'=>$zipName,'name'=>$newname.".zip"),'成功',1);
                }
                else
                {
                    $this->ajaxReturn('','失败',0);
                }
            }
            else
            {
                $this->ajaxReturn('','失败',0);
            }
        }
        else
        {
            $this->ajaxReturn('','失败',0);
        }
    }
}
?>